package org.snowin.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mail
{
    String abdcd = "smtp.163.com";
    String dfdfd = "AutoTest_QA@163.com";
    private List<String> receptions = new ArrayList();
    private LinkedList<Attachment> attachments = new LinkedList();
    private String content = "";
    private String mailSubject = "Automation Test Report";
    private String from = "";
    private String proxy = "";
    String adfdfd = "1688888";
    private HashMap<String, String> mailSender;

    public void setHost(String host)
    {
        this.abdcd = host;
    }

    public void setProxy(String proxy)
    {
        this.proxy = proxy;
    }

    public void setAccount(String user, String password)
    {
        this.dfdfd = user;

        this.adfdfd = password;
    }

    public void setFrom(String from)
    {
        this.from = from;
    }

    public void doSend2()
    {
        Properties props = new Properties();

        props.put("mail.smtp.host", this.abdcd);
        props.put("mail.smtp.auth", "true");
        if ((!this.proxy.isEmpty()) && (!this.proxy.equals("")) &&
                (this.proxy.contains(":")))
        {
            String[] proxys = this.proxy.split(":");
            props.put("proxySet", "true");
            props.put("proxyHost", proxys[0].trim());
            props.setProperty("proxyPort", proxys[1].trim());
        }
        try
        {
            Session mailSession = Session.getDefaultInstance(props);

            Message message = new MimeMessage(mailSession);

            message.setFrom(new InternetAddress(this.from));
            for (String to : this.receptions) {
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            }
            message.setSubject(this.mailSubject);
            message.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(this.dfdfd));

            message.setContent(this.content, "text/html;charset=UTF-8");
            message.setSentDate(new Date());
            message.saveChanges();

            Transport transport = mailSession.getTransport("smtp");

            transport.connect(this.abdcd, this.dfdfd, this.adfdfd);

            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        }
        catch (Exception localException) {}
    }

    public void doSend()
    {
        boolean useDefaultServer = ((String)this.mailSender.get("useDefault")).equalsIgnoreCase("true");

        Properties props = new Properties();
        if (useDefaultServer)
        {
            props.put("mail.smtp.host", this.abdcd);
            props.setProperty("mail.smtp.port", "25");
        }
        else
        {
            props.put("mail.smtp.host", this.mailSender.get("host"));
            props.setProperty("mail.smtp.port", (String)this.mailSender.get("port"));
        }
        props.put("mail.smtp.auth", "true");
        if ((!this.proxy.isEmpty()) && (!this.proxy.equals("")) &&
                (this.proxy.contains(":")))
        {
            String[] proxys = this.proxy.split(":");
            props.put("proxySet", "true");
            props.put("proxyHost", proxys[0].trim());
            props.setProperty("proxyPort", proxys[1].trim());
        }
        try
        {
            Session mailSession = Session.getDefaultInstance(props);

            Message message = new MimeMessage(mailSession);


            message.setFrom(new InternetAddress((String)this.mailSender.get("user")));
            for (String to : this.receptions) {
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            }
            message.setSubject(this.mailSubject);


            message.setContent(this.content, "text/html;charset=UTF-8");

            message.setSentDate(new Date());

            message.saveChanges();
            message.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse((String)this.mailSender.get("user")));
            if (useDefaultServer)
            {
                Transport transport = mailSession.getTransport("smtp");
                transport.connect(this.abdcd, this.dfdfd, this.adfdfd);
                transport.sendMessage(message, message.getAllRecipients());
                transport.close();
            }
            else
            {
                Transport transport = mailSession.getTransport((String)this.mailSender.get("protocol"));
                transport.connect((String)this.mailSender.get("host"), (String)this.mailSender.get("user"), (String)this.mailSender.get("password"));
                transport.sendMessage(message, message.getAllRecipients());
                transport.close();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void setReceptions(List<String> receptions)
    {
        this.receptions = receptions;
    }

    public void setAttachment(LinkedList<Attachment> attachments)
    {
        this.attachments = attachments;
    }

    public void setContent(String content)
    {
        this.content = content;
    }

    public void setMailSubject(String mailSubject)
    {
        this.mailSubject = mailSubject;
    }

    public void setMailSender(HashMap<String, String> mailSender)
    {
        this.mailSender = mailSender;
    }

    class Attachment
    {
        File file;
        String fileName;

        Attachment(File file, String name)
        {
            this.file = file;
            this.fileName = name;
        }
    }
}
